package com.xiaoyu.base.content;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import in.srain.cube.request.JsonData;
import in.srain.cube.request.RequestData;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class IntentRequest extends JsonData {

    protected IntentRequest() {
    }

    public static IntentRequest fromIntent(Intent intent) {
        Map<String, Object> map = new HashMap<>();

        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Set<String> keys = bundle.keySet();
            for (String key : keys) {
                map.put(key, bundle.get(key));
            }
        }
        Uri uri = intent.getData();
        if (uri != null) {
            String query = uri.getEncodedQuery();
            if (!TextUtils.isEmpty(query)) {
                map.putAll(RequestData.parseQuery(query));
            }
        }
        IntentRequest intentRequest = new IntentRequest();
        intentRequest.init(map);
        return intentRequest;
    }
}
